<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-16
 * Time: 9:14
 */

namespace app\api\controller\seller;
use app\common\controller\Api;

use think\Request;
use app\common\logic\OrderLogic;
use app\common\model\Express;
use app\common\model\Pay;
use app\common\model\UnifiedOrder;

class Order extends Api
{
    protected $authenticate=null;
    
    public function index()
    {
        $params = paramForm();
        $logic = new OrderLogic();
        
        if(isset($params['action'])){
            switch ($params['action']){
                case 'express':
                    success(Express::all());
                    break;
                case 'send':
                    $id = paramForm('id',true);
                    $order = $logic->findModel($id);
                    $params['status'] = 2;
                    $params['send_status'] = 2;//修改订单状态和配送状态啊
                    $rt = $order->allowField(true)->save($params,['id'=>$id]);
                    if($rt){
                        success(null);
                    }else{
                        error('发货失败');
                    }
                    break;
                default:
                    break;
            }
            die;
        }
        
        if (isset($params['id'])){
            $this->read($params);
            die;
        }
        
        
        $params['order'] = 'create_time desc';
        $params['with'] = 'module,member';//默认关联购买表吧
        $params['seller'] = $this->getUserId();
        
        //代表是已经收货的了,钱已经给商家了
        if(isset($params['status'])&&$params['status']=='666'){
            $params['status'] = array('in',[4,5]);
        }
        //$logic->setValue('with','purchaser,member');//关联购买表啊
        $list = $logic->getList($params);
        if($list) {
            foreach ($list as $order) {
                $order->purchaser = $logic->getPurchaser($order->id);
            }
        }
        success($list);
        
    }
    
    public function read($params){
        $id = $params['id'];
        $logic = new OrderLogic();
        $logic->setValue('with','module,member');
        
        $order = $logic->findModel($id);
        if(!$order){
            error('订单不存在');
        }
        if($order->seller!=$this->getUserId()){
            error('只能操作自己的订单');
        }
        
        $order->unifiedOrder = $logic->getUnifiedOrder($id);
        $order->purchaser = $logic->getPurchaser($id);
        $order->pay = Pay::get(['out_trade_no'=>$order->unifiedOrder->out_trade_no]);
       success($order);
    }
    

    public function send(){
        $rule = [
            'send_no|订单号'=>'require',
            'send_type|物流'=>'require',
            'send_type_id|物流序号'=>'require',
        ];
        $params = paramForm();
        $this->validate($params,$rule);
        
    }
    
    
    
    
}